package com.buybuyyi.stylistcrm.user.dao.impl;

import com.buybuyyi.stylistcrm.consts.DateFormatPattern;
import com.buybuyyi.stylistcrm.user.dao.AuditingDao;
import com.buybuyyi.stylistcrm.user.mapper.StylistUserProfileMapper;
import com.buybuyyi.stylistcrm.user.mapper.UserProfileMapper;
import com.buybuyyi.stylistcrm.user.po.*;
import com.buybuyyi.stylistcrm.utils.DateUtil;
import com.buybuyyi.stylistcrm.utils.StringUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import stylistcrm.core.services.order.exception.BizException;
import stylistcrm.core.services.security.dto.UserDto;
import stylistcrm.core.services.security.service.SecurityService;
import stylistcrm.core.services.user.EnumUserRole;
import stylistcrm.core.services.user.EnumUserType;
import stylistcrm.core.services.user.profile.Gender;
import stylistcrm.core.services.user.requisition.Requisition;
import stylistcrm.core.services.user.requisition.RequisitionDto;

import java.util.Date;

/**
 * Created by yanwc on 2016/10/9.
 */
@Repository
public class StylistAuditingDaoImpl implements AuditingDao {
    private static Logger logger = LogManager.getLogger(StylistAuditingDaoImpl.class);

    @Autowired
    StylistUserProfileMapper stylistUserProfileMapper;

    @Autowired
    UserProfileMapper userProfileMapper;

    @Autowired
    SecurityService securityService;

    @Override
    public boolean isAudit(RequisitionDto requisition) {
        if(requisition == null){
            logger.warn("审核申请单为空！");
            return false;
        }
        if(EnumUserType.STYLIST.getCode().equals(requisition.getUserType())){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean pass(RequisitionDto requisition, UserDto userDto) throws BizException {
        if(requisition == null){
            logger.error("审核失败，申请单为空值");
            return false;
        }
        UserProfile userProfile = new UserProfile();
        //插入父类
        userProfile.setCreateTime(new Date());
        userProfile.setBankAccountNumber(requisition.getBankAccountNumber());
        if(StringUtil.isNotBlank(requisition.getBirthDay())){
            userProfile.setBirthDay(DateUtil.parseDate(requisition.getBirthDay(), DateFormatPattern.PATTERN_YYYY_MM_DD));
        }
        if(null!=requisition.getGender()){
            userProfile.setGender(Gender.getEnum(requisition.getGender()));
        }
        userProfile.setNickName(requisition.getNickName());
        userProfile.setRealName(requisition.getRealName());
        userProfile.setHeadImageUrl(requisition.getHeadImageUrl());
        userProfile.setCreatorId(userDto.getId());
        userProfile.setCreatorName(userDto.getUserName());
        userProfile.setSecUserId(requisition.getSecUserId());
        userProfileMapper.insert(userProfile);
        //插入子类信息
        StylistUserProfileWithBLOBs stylistUserProfile = new StylistUserProfileWithBLOBs();
        stylistUserProfile.setIsPartTime("1".equals(requisition.getIsPartTime()));
        stylistUserProfile.setExperience(requisition.getExperience());
        stylistUserProfile.setResume(requisition.getResume());
        stylistUserProfile.setId(userProfile.getId());
        stylistUserProfileMapper.insert(stylistUserProfile);
        //插入角色信息
        securityService.saveUserRole(requisition.getSecUserId(), EnumUserRole.STYLIST.getCode().longValue());
        return true;
    }

}
